package com.om.opensourway;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Utils.HttpClientUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.yaml.snakeyaml.Yaml;

/**
 * 分支发布版本和发布时间解析
 */
public class ParseUpstreamInfo {
    public static void main(String[] args) {
        parseAndToES(args[0], args[1]);
    }


    private static void parseAndToES(String filePath, String indexName) {
        Map<String, String> res = new HashMap<>();
        RestHighLevelClient restHighLevelClient = HttpClientUtils.restClient();
        BulkRequest request = new BulkRequest();

        Yaml yaml = new Yaml();
        File[] files = new File(filePath).listFiles();
        assert files != null;
        for (File file : files) {
            try {
                FileInputStream fis = new FileInputStream(file);
                Map map = yaml.loadAs(fis, Map.class);
                List<String> gitTags;
                try {
                    gitTags = (List<String>) map.getOrDefault("git_tag", new ArrayList<String>());
                } catch (Exception ex) {
                    continue;
                }

                if (gitTags == null) {
                    continue;
                }
                for (String gitTag : gitTags) {
                    String[] s = gitTag.split(" ");
                    if (s.length != 2) {
                        continue;
                    }
                    String repo_name = file.getName().replace(".yaml", "");
                    String date = s[0] + " 00:00:00";

                    int start = -1;
                    for (int i = s[1].indexOf(".") - 1; i >= 0; i--) {
                        char c = s[1].charAt(i);
                        if (c < '0' || c > '9') {
                            start = i;
                            break;
                        }
                    }
                    String version = s[1].substring(start + 1);

                    res.put("repo_name", repo_name);
                    res.put("version_time", date);
                    res.put("version", version);
                    request.add(new IndexRequest(indexName, "_doc", repo_name + version).source(res));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (request.requests().size() != 0) {
            try {
                restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    restHighLevelClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}